﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace BLL
{
    public class DatHangBLL
    {
        public List<DatHang> GetAllDatHang()
        {
            ChamCongEntities cc = new ChamCongEntities();
            return cc.DatHangs.Include("DonVi").Include("SanPham").ToList<DatHang>();
        }
        public DatHang GetDatHangById(int id)
        {
            using (var cc = new ChamCongEntities())
            {
                return cc.DatHangs.Include("DonVi").Include("SanPham").Where(dh => dh.DatHangId == id).FirstOrDefault();
            }
        }
        public List<DatHang> SearchByTen(string key)
        {
            using (var context = new ChamCongEntities())
            {
                var idParam = new SqlParameter
                {
                    ParameterName = "kytu",
                    Value = key
                };

                return context.DatHangs.SqlQuery("exec SearchByTenInDatHang @kytu", idParam).ToList<DatHang>();
            }
        }
       
        public void InsertDatHang(DatHang datHang)
        {
            using (var cc = new ChamCongEntities())
            {
                cc.Entry(datHang).State = EntityState.Added;
                cc.SaveChanges();
            }
        }
        public void UpdateDatHang(DatHang datHang)
        {
            using (var cc = new ChamCongEntities())
            {
                cc.Entry(datHang).State = EntityState.Modified;
                cc.SaveChanges();
            }
        }
        public void DeleteDatHang(DatHang datHang)
        {
            using (var cc = new ChamCongEntities())
            {
                cc.Entry(datHang).State = EntityState.Deleted;
                cc.SaveChanges();
            }
        }
    }
}
